<?php
class Vote
{

    public $db;
    public $dsn = 'mysql:dbname=vote;host=127.0.0.1';
    public $user = 'root';
    public $password = 'sns20070804';

    public function con ()
    {
        $this->db = null;
        try
        {
            $this->db = new PDO($this->dsn, $this->user, $this->password, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)); //,array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => "true",PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
            $this->db->exec("SET NAMES utf8");
        }
        catch (PDOException $e)
        {
            echo 'Connection failed: ' . $e->getMessage();
        }
    }

	public function __construct()
	{
		$this->con();
	}
	

	public function login($user_account)
	{
		$sth = $this->db->prepare("select * from vote_user where user_account = ?");
		$sth->execute(array("$user_account"));
		$res = $sth->fetchAll(PDO::FETCH_ASSOC);
		if(!empty($res[0]['id'])):
		    $_SESSION['user_account'] = $res[0]['user_account'];
			header('location:vote.php');
		else:
			return $flag = '<span style="color:red">你输入的学号有错误</span>';
		endif;
	}

	public  function listInfo($type)
	{
		$sql = "select * from vote_position where position_name = ?";
		$sth = $this->db->prepare("$sql");
		$sth->execute(array("$type"));
		return $sth->fetchAll(PDO::FETCH_ASSOC);
	}

	public function ajaxVote($user_account_id,$user_id,$PN)
	{
		$sth = $this->db->prepare("select id from vote_num where user_account_id = ? and position_name = ?");
		$sth->execute(array("$user_account_id","$PN"));
		$res = $sth->fetchAll(PDO::FETCH_ASSOC);
		if(!empty($res)):
			return 'hasVoted';	
		else:
			$this->db->exec("insert into vote_num (`user_account_id`,`user_position_id`,`position_name`) values ('$user_account_id','$user_id','$PN')");

			$this->db->exec("update vote_position set total_num = total_num + 1 where id = '$user_id'");

			return	$list = $this->listInfo($PN);
			
		endif;
	}

	public function  ajaxVoteNum($user_position_id,$PN)
	{
		$sth = $this->db->prepare("select id from vote_num where user_position_id = ? and position_name = ?");
		$sth->execute(array("$user_position_id","$PN"));
		return $sth->rowCount();
	}

	public function loginOut()
	{
		unset($_SESSION['user_account']);
		header('location:index.php');
	}

	public function judgeLogin()
	{
		if(empty($_SESSION['user_account'])):
			header('location:index.php');
		endif;
	}
	
	public function judgeVoted($type)
	{
		$sth = $this->db->prepare("select id from vote_num where position_name = ? and user_account_id = ?");
		$sth->execute(array("$type","$_SESSION[user_account]"));
		return $sth->rowCount();
	}
	
	public function voteCount()
	{
		$sth = $this->db->prepare("select * from vote_user where user_account not in (select distinct user_account_id from vote_num)");
		$sth->execute();
		return $res = $sth->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function listMessage()
	{
		$sth = $this->db->prepare("select * from vote_bbs order by create_time desc");
		$sth->execute();
		return $res = $sth->fetchAll(PDO::FETCH_ASSOC);
	}
	
	public function addMessage($user_account,$content)
	{
		if(!empty($content)):
		$this->db->exec("insert into vote_bbs (`user_account_id`,`content`) values ('$user_account','$content')");
		//echo "<script>alert('留言成功');</script>";
		//sleep(2);
		header('location:bbs.php');
		endif;
	}
	
}
?>
